import { Animation, AnimationClip } from 'cc'
import { StateMachine, getInitParamTrigger } from "../../Base/StateMachine"
import { State } from '../../Base/State'
import { ExplosionManager } from './ExplosionManager'
import { ObjectPoolManager } from '../../Runtime/ObjectPoolManager'
import { StateTypeEnum } from '../../Common'

export class ExplosionStateMachine extends StateMachine {

    entity: ExplosionManager
    init(entity: ExplosionManager) {
        this.animationComponent = this.node.addComponent(Animation)
        this.entity = entity
        this.initParam()
        this.initState()
        this.initAnimationEvent()
    }

    initParam() {
        this.params.set(StateTypeEnum.idle, getInitParamTrigger())
        this.params.set(StateTypeEnum.none, getInitParamTrigger())
    }

    initState() {
        this.stateMachines.set(StateTypeEnum.idle, new State(this, this.entity.type, StateTypeEnum.idle, AnimationClip.WrapMode.Normal, 20))
        this.stateMachines.set(StateTypeEnum.none, new State(this, this.entity.type, StateTypeEnum.none, AnimationClip.WrapMode.Normal, 20))
    }

    initAnimationEvent() {
        this.animationComponent.on(Animation.EventType.FINISHED, () => {
            this.entity.state = StateTypeEnum.none
            ObjectPoolManager.Ins.poolFree(this.entity)
        })
    }

    run() {

        switch (this.currencyState) {
            case this.stateMachines.get(StateTypeEnum.idle):
            case this.stateMachines.get(StateTypeEnum.none):
                if (this.params.get(StateTypeEnum.idle).value) {
                    this.currencyState = this.stateMachines.get(StateTypeEnum.idle)
                } else if (this.params.get(StateTypeEnum.none).value) {
                    this.currencyState = this.stateMachines.get(StateTypeEnum.none)
                }
                break;
            default:
                this.currencyState = this.stateMachines.get(StateTypeEnum.idle)
                break;
        }

    }

}